package com.cz.zslu.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.cz.zslu.common.ReturnObject;
import com.cz.zslu.entity.Cms;
import com.cz.zslu.entity.SysUser;
import com.cz.zslu.entity.ZslNews;
import com.cz.zslu.interceptor.BaseContextHandler;
import com.cz.zslu.interceptor.IgnoreAuth;
import com.cz.zslu.service.ICmsService;
import com.cz.zslu.service.ISysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 内容 前端控制器
 * </p>
 *
 * @author 森森
 * @since 2018-12-18
 */
@RestController
@Api(description = "新闻模块")
@RequestMapping("/cms")
public class CmsController {
    @Autowired
    private ICmsService iCmsService;
    @Autowired
    private ISysUserService iSysUserService;


    @ApiOperation("获取店铺咨询分页")
    @IgnoreAuth
    @RequestMapping(value = "/getAllNewPage", method = RequestMethod.POST)
    public ReturnObject<List<Cms>> getAllNewPage(@ApiParam("每页条数") @RequestParam(defaultValue = "10") int limit,
                                                 @ApiParam("页数") @RequestParam(defaultValue = "1") int offset,
                                                 @ApiParam(" 类型（0：企业动态，1：荣誉资质，2：企业简介，3：友情链接）") int type,
                                                 @ApiParam("店铺id") Long storeId) {
        EntityWrapper<Cms> ew = new EntityWrapper<>();
        ew.eq("type", type);
        ew.eq("store_id", storeId);
        ew.orderBy("crt_time", false);
        Page<Cms> obj = iCmsService.selectPage(new Page<>(offset, limit), ew);
        return new ReturnObject<>(0, "获取成功", obj.getTotal(), obj.getPages(), obj.getRecords());
    }


    @ApiOperation("获取详情")
    @RequestMapping(value = "/findInfoById", method = RequestMethod.POST)
    public ReturnObject<Cms> findInfoById(@ApiParam("id") @RequestParam Long id) {

        return new ReturnObject<>(0, "获取成功", iCmsService.selectById(id));
    }

    @ApiOperation("新增")
    @RequestMapping(value = "/saveNewInfo", method = RequestMethod.POST)
    public ReturnObject<Cms> saveNewInfo(@ApiParam("标题") String title,
                                         @ApiParam("内容") @RequestParam String content,
                                         @ApiParam("图片地址") @RequestParam String picUrl,
                                         @ApiParam(" 类型（0：企业动态，1：荣誉资质，2：企业简介，3：友情链接）") int type) {
        SysUser sysUser = iSysUserService.selectById(BaseContextHandler.getUserID());
        Cms info = new Cms();
        info.setTitle(title);
        info.setContent(content);
        info.setPicUrl(picUrl);
        info.setType(type);
        info.setUserId(BaseContextHandler.getUserID());
        info.setStoreId(sysUser.getStoreId());
        iCmsService.insert(info);
        return new ReturnObject<>(0, "新增成功");
    }

    @ApiOperation("修改")
    @RequestMapping(value = "/editNewInfo", method = RequestMethod.POST)
    public ReturnObject<Cms> editNewInfo(@ApiParam("标题") String title,
                                         @ApiParam("id") @RequestParam Long id,
                                         @ApiParam("内容") @RequestParam String content,
                                         @ApiParam("图片地址") @RequestParam String picUrl,
                                         @ApiParam(" 类型（0：企业动态，1：荣誉资质，2：企业简介，3：友情链接）") int type) {
        SysUser sysUser = iSysUserService.selectById(BaseContextHandler.getUserID());
        Cms info = iCmsService.selectById(id);
        info.setTitle(title);
        info.setContent(content);
        info.setPicUrl(picUrl);
        info.setType(type);
        info.setUserId(BaseContextHandler.getUserID());
        info.setStoreId(sysUser.getStoreId());
        iCmsService.updateById(info);
        return new ReturnObject<>(0, "新增成功");
    }

    @ApiOperation("删除")
    @RequestMapping(value = "/delNewById", method = RequestMethod.POST)
    public ReturnObject<ZslNews> delNewById(@ApiParam("id") @RequestParam Long id) {
        iCmsService.deleteById(id);
        return new ReturnObject<>(0, "删除成功");
    }

    @ApiOperation("批量删除")
    @RequestMapping(value = "/delNewByIds", method = RequestMethod.POST)
    public ReturnObject<ZslNews> delNewByIds(@ApiParam("ids") @RequestParam Long[] ids) {
        iCmsService.deleteBatchIds(Arrays.asList(ids));
        return new ReturnObject<>(0, "删除成功");
    }

}

